package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationExample;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.TbSpecificationServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

@com.alibaba.dubbo.config.annotation.Service
@Service
public class TbSpecificationServiceImpl implements TbSpecificationServiceI {

    //规格表
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    //规格选项表
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    //事物管理(注解)
    @Transactional
    public List<TbSpecification> getFindAll() {
        return tbSpecificationMapper.getFindAll();
    }

    @Override
    //事物管理(注解)
    @Transactional
    public void addTbSpecification(TbSpecification tbSpecification, List<TbSpecificationOption> specificationOptions) {
        //增加并返回ID(规格)
        tbSpecificationMapper.addTbSpecificationId(tbSpecification);
        //遍历所有增加获取的值
        Stream<TbSpecificationOption> stream = specificationOptions.stream();
        Consumer<TbSpecificationOption> consumer = tbSpecificationOption ->{
            tbSpecificationOption.setSpecId(tbSpecification.getId());
            tbSpecificationOptionMapper.insertSelective(tbSpecificationOption);
        };
        stream.forEach(consumer);
    }

    @Override
    //事物管理(注解)
    @Transactional
    public void deleteTbSpecification(String id) {
        tbSpecificationMapper.deleteTbSpecificationById(id);
    }

    @Override
    //事物管理(注解)
    @Transactional
    public void deleteTbSpecificationOption(String id) {
        tbSpecificationOptionMapper.deleteTbSpecificationOptionById(id);
    }

    @Override
    //事物管理(注解)
    @Transactional
    public List<TbSpecificationOption> selectTbSpecificationOptionById(Integer id) {

        //根据条件查询规格选项表
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id.longValue());
        return tbSpecificationOptionMapper.selectByExample(example);
    }

    //根据id同部查询规格表
    @Override
    //事物管理(注解)
    @Transactional
    public TbSpecification selectTbSpecificationById(Integer bei) {
        return tbSpecificationMapper.selectByPrimaryKey(bei.longValue());
    }

    @Override
    //事物管理(注解)
    @Transactional
    public void updateTbSpecification(TbSpecification tbSpecification, List<TbSpecificationOption> specificationOptions) {
        //根据id修改规格表
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        //根据规格表id  条件删除中间表
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        tbSpecificationOptionMapper.deleteByExample(example);
        //遍历所有增加获取的值
        Stream<TbSpecificationOption> stream = specificationOptions.stream();
        Consumer<TbSpecificationOption> consumer = tbSpecificationOption ->{
            tbSpecificationOption.setSpecId(tbSpecification.getId());
            tbSpecificationOptionMapper.insertSelective(tbSpecificationOption);
        };
        stream.forEach(consumer);
    }
}
